const app = getApp();
var memberUtil=require('../../utils/memberUtil');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    customItem: '',
    region:[],
    isDefault:0,
    receiverProvince:"",
    recevierCity:"",
    recevierDistrictCounty:"",
    addressType:'add'

  },
  bindRegionChange: function (e) {
    var that=this;
    var diqu=e.detail.value;
    var receiverProvince=diqu[0],recevierCity=diqu[1],recevierDistrictCounty=diqu[2];
    console.log(receiverProvince+"="+recevierCity+"="+recevierDistrictCounty);
    that.setData({
      receiverProvince:receiverProvince,
      recevierCity:recevierCity,
      recevierDistrictCounty:recevierDistrictCounty,
      region: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this;
    var addressId=options.adId;
    var addressType=options.type;
    var address=[];
    if(addressType=='edit'){
      var addressIdJson={addressId:addressId}
      wx.request({
        url: app.globalData.baseUrl+'/member/receivingAddress/findAddressById',
        header: app.globalData.headerPost,
        method: 'POST',
        data:JSON.stringify(addressIdJson),
        success:function(res){
          if(Number(res.data.status)==200) {
            var region=[res.data.data.receiverProvince,res.data.data.recevierCity,res.data.data.recevierDistrictCounty];
            that.setData({
              receiverProvince:res.data.data.receiverProvince,
              recevierCity:res.data.data.recevierCity,
              recevierDistrictCounty:res.data.data.recevierDistrictCounty,
              region:region
            })
            that.setData({
              address: res.data.data
            })
          }else{
            that.setData({
              address:[]
            })
          }
        }
      })
    }
    that.setData({
      userInfo:wx.getStorageSync("loginUserInfo").userInfo==undefined?"":wx.getStorageSync("loginUserInfo").userInfo,
      addressType:options.type
    })
    if(that.data.userInfo.id==undefined) {
      memberUtil.getLogin(app,0,1,that,2);
    }
  },
  bindSave: function (e) {
    var that = this;
    var receiverName = e.detail.value.receiverName;
    if (receiverName == "" || receiverName ==null) {
      wx.showModal({
        title: '错误',
        content: '收货人不能为空',
        showCancel: false
      })
      return false;
    } else if (!(/^[a-zA-Z\u4e00-\u9fa5]+$/.test(receiverName))){
      wx.showModal({
        title: '错误',
        content: '收货人只能是汉字或字母',
        showCancel: false
      })
      return false;
    }

    var receiverMobilePhone = e.detail.value.receiverMobilePhone;
    if (receiverMobilePhone == "" || receiverMobilePhone == null) {
      wx.showModal({
        title: '错误',
        content: '手机号码不能为空',
        showCancel: false
      })
      return false;
    } else if (!(/^1[34578]\d{9}$/.test(receiverMobilePhone))){
      wx.showModal({
        title: '错误',
        content: '请输入正确的手机号码',
        showCancel: false
      })
      return false;
    }

    var receiverAddressDetail = e.detail.value.receiverAddressDetail;
    if (receiverAddressDetail == "" || receiverAddressDetail == null) {
      wx.showModal({
        title: '错误',
        content: '详细地址不能为空',
        showCancel: false
      })
      return false;
    }
    if(that.data.receiverProvince=="" || that.data.recevierCity=="" || that.data.recevierDistrictCounty==""){
      wx.showModal({
        title: '错误',
        content: '所在地区不能为空',
        showCancel: false
      })
      return false;
    }
    var isDefault = e.detail.value.isDefault;
    var requestJson={
      memberId:that.data.userInfo.id,
      receiverName: receiverName,
      receiverMobilePhone: receiverMobilePhone,
      receiverProvince:that.data.receiverProvince,
      recevierCity:that.data.recevierCity,
      recevierDistrictCounty:that.data.recevierDistrictCounty,
      receiverAddressDetail: receiverAddressDetail,
      receiverZipcode: "",
      isDefault: that.data.isDefault
    }
    console.log("requestJson="+JSON.stringify(requestJson))
    if(that.data.addressType=="edit"){
      var receivingAddressJson=requestJson;
      receivingAddressJson["id"]=that.data.address.id;
      wx.request({
        url: app.globalData.baseUrl + '/member/receivingAddress/updateReceivingAddress',
        header: app.globalData.headerPost,
        data: JSON.stringify(receivingAddressJson),
        method: 'POST',
        success: function (res) {
          var likeObj = "";
          if (res.data.status == 200) {
            wx.showToast({
              title: '收货地址修改成功',
              icon: 'none',
              duration: 2000
            })
            wx.redirectTo({
              url: '/pages/my/myaddress'
            })
          }

        }
      })
    }else if(that.data.addressType=='add') {
      wx.request({
        url: app.globalData.baseUrl + '/member/receivingAddress/addReceivingAddress',
        header: app.globalData.headerPost,
        data: JSON.stringify(requestJson),
        method: 'POST',
        success: function (res) {
          console.log("添加地址成功" + JSON.stringify(res.data));
          var likeObj = "";
          if (res.data.status == 200) {
            wx.showToast({
              title: '收货地址添加成功',
              icon: 'none',
              duration: 1000
            })
            wx.redirectTo({
              url: '/pages/my/myaddress'
            })
          }

        }
      })
    }
  },
  switchFu: function (e) {
    var that=this;
    var value= e.detail.value;
    if(value){
      that.setData({
        isDefault: 0
      })
    }else{
      that.setData({
        isDefault:1
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var that=this;
    return {
      title: '我的二维码',
      path: "/pages/shop/shareSuccess?parentId=" + that.data.userInfo.id + "&enterType=0",
      imageUrl: app.globalData.loginUserInfo.erweimaUrl
    }
  }
})